﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Xml.Serialization;
using AppConfig;
using ApplicationConfiguration;

namespace GUIServerConnectorAPI
{
    public class GUIServerConnector
    {
        public static readonly string contextPath = "GUIServerConnector";

        private static GUIServerConnector instance = null;

        /// <summary>
        /// Acquires the singleton instance for the server connector
        /// </summary>
        /// <returns>The singleton instance</returns>
        public static GUIServerConnector getInstance()
        {
            if (instance == null)
            {
                AppConfiguration appConfiguration = AppConfiguration.getInstance();
                string hostName = appConfiguration.getProperty("GUIServerConnector", "hostName");
                int port = Int32.Parse(appConfiguration.getProperty("GUIServerConnector", "port"));
                instance = new GUIServerConnector(hostName, port);
            }
            return instance;
        }

        private string baseConnectionURL;

        /// <summary>
        /// Creates the singleton instance for the server connector
        /// </summary>
        /// <param name="hostName">The name of the host to connect to</param>
        /// <param name="port">The port to to use for connecting</param>
        private GUIServerConnector(string hostName, int port)
        {
            baseConnectionURL = "tcp://" + hostName + ":" + port + "/" + contextPath;

            TcpChannel channel = new TcpChannel();
            ChannelServices.RegisterChannel(channel, true);
        }

        /// <summary>
        /// Acquires a remote instance for the Authenticator
        /// </summary>
        /// <returns>The instance of the Authenticator</returns>
        public IAuthenticator getAuthenticator()
        {
            return (IAuthenticator) Activator.GetObject(typeof(IAuthenticator), baseConnectionURL + "/" + typeof(IAuthenticator).Name);
        }

        /// <summary>
        /// Acquires a remote instance for the ServerInterface
        /// </summary>
        /// <returns>The instance of the ServerInterface</returns>
        public IServerInterface getServerInterface()
        {
            return (IServerInterface) Activator.GetObject(typeof(IServerInterface), baseConnectionURL + "/" + typeof(IServerInterface).Name);
        }

        /// <summary>
        /// Acquires a remote instance for the ManagerInterface
        /// </summary>
        /// <returns>The instance of the ManagerInterface</returns>
        public IManagerInterface getManagerInterface()
        {
            return (IManagerInterface) Activator.GetObject(typeof(IManagerInterface), baseConnectionURL + "/" + typeof(IManagerInterface).Name);
        }
    }
}
